#用于存储学生姓名和成绩的字典
students ={}
#定义记录学生成绩的函数
def add_student():
    name = input("请输入学生姓名: ")
    student_id = input("请输入学生学号: ")
    for student in students:
        if student["student_id"] == student_id:
            print("学号已存在，请重新输入！")
            return
    course = input("请输入课程名称: ")
    try:
        score = int(input("请输入学生成绩(0-100分): "))
        if score < 0 or score > 100:
            raise ValueError
    except ValueError:
        print("成绩输入错误，请输入0-100之间的整数！")
        return
    student = {
        "name": name,
        "student_id": student_id,
        "course": course,
        "score": score
    }
    students.append(student)
    print("学生成绩录入成功！")

def query_student():
    option = input("请选择查询方式(1:按学生姓名，2:按学号，3:按课程名称): ")
    if option == "1":
        name = input("请输入学生姓名: ")
        found = False
        for student in students:
            if student["name"] == name:
                print(f"姓名: {student['name']}, 学号: {student['student_id']}, 课程: {student['course']}, 成绩: {student['score']}")
                found = True
        if not found:
            print("未找到该学生的成绩记录！")
    elif option == "2":
        student_id = input("请输入学生学号: ")
        found = False
        for student in students:
            if student["student_id"] == student_id:
                print(f"姓名: {student['name']}, 学号: {student['student_id']}, 课程: {student['course']}, 成绩: {student['score']}")
                found = True
        if not found:
            print("未找到该学生的成绩记录！")
    elif option == "3":
        course = input("请输入课程名称: ")
        found = False
        for student in students:
            if student["course"] == course:
                print(f"姓名: {student['name']}, 学号: {student['student_id']}, 课程: {student['course']}, 成绩: {student['score']}")
                found = True
        if not found:
            print("未找到该课程的成绩记录！")
    else:
        print("输入错误，请输入1、2或3！")

def statistics():
    course = input("请输入要统计的课程名称: ")
    scores = [student["score"] for student in students if student["course"] == course]
    if not scores:
        print(f"未找到课程 {course} 的成绩记录！")
        return
    average = sum(scores) / len(scores)
    highest = max(scores)
    lowest = min(scores)
    print(f"课程 {course} 的平均分: {average}, 最高分: {highest}, 最低分: {lowest}")

def display_menu():
    print("学生成绩管理系统")
    print("1. 录入学生成绩")
    print("2. 查询学生成绩")
    print("3. 统计成绩")
    print("4. 退出")

def main():
    while True:
        display_menu()
        choice = input("请输入菜单选项的序号: ")
        if choice == "1":
            add_student()
        elif choice == "2":
            query_student()
        elif choice == "3":
            statistics()
        elif choice == "4":
            print("感谢使用，程序已退出！")
            break
        else:
            print("输入错误，请输入1、2、3或4！")

if __name__ == "__main__":
    main()